package xyz.zhouzhaodong.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.transaction.annotation.Transactional;
import xyz.zhouzhaodong.crm.dto.StationDto;
import xyz.zhouzhaodong.crm.entity.Department;
import xyz.zhouzhaodong.crm.entity.Staff;
import xyz.zhouzhaodong.crm.entity.Station;
import xyz.zhouzhaodong.crm.mapper.StationMapper;
import xyz.zhouzhaodong.crm.service.IStationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import xyz.zhouzhaodong.crm.util.ExceptionEnum;
import xyz.zhouzhaodong.crm.util.RestMessage;

import javax.annotation.Resource;
import java.beans.Transient;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

/**
 * <p>
 * 岗位表 服务实现类
 * </p>
 *
 * @author zhouzhaodong
 * @since 2020-06-10
 */
@Service
public class StationServiceImpl extends ServiceImpl<StationMapper, Station> implements IStationService {

    @Resource
    StationMapper stationMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage addStation(StationDto stationDto) {
        try {
            RestMessage restMessage = selectStation(stationDto);
            if (restMessage.getCode() != 0) {
                return restMessage;
            }
            // 随机生成id
            String id = UUID.randomUUID().toString().replaceAll("-", "");
            Station station = new Station();
            station.setId(id);
            station.setName(stationDto.getName());
            station.setDepartmentId(stationDto.getDepartmentId());
            // 创建人为登录人
            station.setCreateUser(stationDto.getStaffId());
            station.setCreateTime(new Date());
            stationMapper.insert(station);
            return RestMessage.success(id);
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage deleteStation(String id, String staffId) {
        try {
            Station station = new Station();
            station.setId(id);
            station.setFlag(1);
            station.setUpdateUser(staffId);
            station.setUpdateTime(new Date());
            stationMapper.updateById(station);
            return RestMessage.success();
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage updateStation(StationDto stationDto) {
        try {
            RestMessage restMessage = selectStation(stationDto);
            if (restMessage.getCode() != 0) {
                return restMessage;
            }
            stationDto.setUpdateTime(new Date());
            stationMapper.updateStation(stationDto);
            return RestMessage.success();
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    public RestMessage selectOne(String id) {
        Station station = stationMapper.selectById(id);
        return RestMessage.success(station);
    }

    @Override
    public RestMessage selectList(StationDto stationDto) {
        if(stationDto.getFlag() == null){
            stationDto.setFlag(0);
        }
        if (stationDto.getPageNo() == 0 && stationDto.getPageSize() == 0) {
            // 不分页返回查询数据
            List<StationDto> list = stationMapper.selectStationList(stationDto);
            return RestMessage.success(list);
        } else {
            Page<Station> page = new Page<>(stationDto.getPageNo(), stationDto.getPageSize());
            IPage<StationDto> list = stationMapper.selectStationList(stationDto, page);
            return RestMessage.success(list);
        }
    }

    public RestMessage selectStation(StationDto stationDto) {
        // 首先需要查询是否存在该岗位
        QueryWrapper<Station> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", stationDto.getName());
        queryWrapper.eq("department_id", stationDto.getDepartmentId());
        if(stationDto.getFlag() == null){
            queryWrapper.eq("flag", 0);
        } else {
            queryWrapper.eq("flag", stationDto.getFlag());
        }
        List<Station> list = stationMapper.selectList(queryWrapper);
        if (list.size() > 0 && list.get(0) != null) {
            return new RestMessage(ExceptionEnum.STATION_NAME.getCode(), ExceptionEnum.STATION_NAME.getMsg());
        }
        return RestMessage.success();
    }
}
